function inStru=sampleAssignments(inStru,flagNoErrors) 
%% sampleAssignments Description 
%
% function inStru=sampleAssignments(inStru,flagNoErrors) 
%
%% Input 
% *inStru*, structure must contain two fields 
%
% 1) *.sampleVec:* numeric vector with the whole sample 
%
% 2) *.subSamples:* [NB NC] cell array with dates at which the sub-samples
% start in column 1 and subsample ends in column 2. Leave empty if there
% are no subsamples.  
%
% *flagNoErrors:* default is 0, so errors will be produced. This should
% always be the case for estimation. When extracting subsamples for graphs,
% as in estimaStates.m, can set it to one to allow for warnings instead. 
% 
% If flagNoErrors=1, the errors allowed are as follows 
% 1) First subSample does not begin at first date of SampleVec 
% 2) Last subSample does not end at last date of SampleVec 
% 3) SubSamples are not contigous and hence add up to the total number of
% observations 
%
%% Output 
%
% 1) *.sampleCell:* cell with the whole sample in string form 
%
% 2) *.subSampleN:* [NB 1] vector with the lengths of the sub-samples 
%
% 3) *.subSamplePos:* [NB 2] matrix with the position at which each sample
%  begings and ends 
%
% 4) *.subSampleCell:* [NB 1] cell with the names of the subsamples in string
%  form 
%
% 5) *.tauVec:* [NT 1] vector indicating which observation does each sample
% belong to 
% 
% A. Justiniano Dec 23 2011
%
%% I. Create cell vector
if nargin < 2 || isempty(flagNoErrors)==true || flagNoErrors~=1 
    flagNoErrors=0; 
end 
[sampleCell,frequencyString]=sample2date(inStru.sampleVec);
inStru.frequency=frequencyString;
inStru.sampleCell=sampleCell;
if isfield(inStru,'subSamples')==0
    inStru.tauVec=ones(length(inStru.sampleVec),1);
    return
end
%% II. Begin Subsamples 
if isempty(inStru.subSamples)==false;
    [NB NC]=size(inStru.subSamples);
    if NC~=2
        error('subSamples must be a NBx2 cell')
    end
    flagSubSamples=1;
else
    flagSubSamples=0;
    inStru.tauVec=ones(size(inStru.sampleVec,1));
    inStru.subSampleCell=[];
    inStru.subSamplePos =[];
    inStru.subSampleN   =[];
    return; 
end
subSampleN=zeros(NB,1);
subSampleCell=cell(NB,1);
subSamplePos=zeros(NB,2);
for ii=1:NB
    startCell=inStru.subSamples(ii,1);
    endCell  =inStru.subSamples(ii,2);
    subSampleCell(ii,:)={[char(startCell),'-',char(endCell)]};
    dispaj('Sample ',char(subSampleCell(ii,:)) );
    startPos =find( strcmpi(char(inStru.subSamples(ii,1)),sampleCell) == 1);
    endPos   =find( strcmpi(char(inStru.subSamples(ii,2)),sampleCell) == 1); 
    if isempty(startPos)
        error('Empty Starting Position');
    end
    if isempty(endPos)
        error('Empty Ending Position')
    end
    subSamplePos(ii,:)=[startPos endPos];
    if ii > 1
        if subSamplePos(ii,1)-subSamplePos(ii-1,2)~=1
            if flagNoErrors==0;
                error('Non Continuous samples')
            else
                warning('Non Continuous samples');
            end
        end
    end
    subSampleN(ii)=endPos-startPos+1;
end
if subSamplePos(1,1)~=1
    if flagNoErrors==0;
        error('Sub-Samples do not beging at beginning of the sample')
    else
        warning('Sub-Samples do not beging at beginning of the sample')
    end
end
if subSamplePos(end,2)~=size(inStru.sampleVec)
    if flagNoErrors==0;        
        error('Sub-Samples do not end at last Point of the sample')
    else
        warning('Sub-Samples do not end at last Point of the sample')
    end        
end
if sum(subSampleN)~=length(inStru.sampleVec);
    if flagNoErrors==0;
        error('Sub-Samples do not add up to Total Number of Obs')
    else
        warning('Sub-Samples do not add up to Total Number of Obs')
    end
end
[tauVec,boundsMat]=genTauVec(subSampleN);
if ~isequal(subSamplePos,boundsMat)
    if flagNoErrors==0
    error('Bounds not properly defined')
    end
end
inStru.tauVec=tauVec;
inStru.subSampleCell=subSampleCell;
inStru.subSamplePos =subSamplePos;
inStru.subSampleN   =subSampleN;
end